﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using csvm.datamodel.contract;
using csvm.pagebuilder.contract;

namespace csvm.pagebuilder
{
    public class PageBuilder : IPageBuilder
    {
        private readonly int pageSize;

        internal PageBuilder(int pageSize)
        {
            this.pageSize = pageSize;
        }

        public PageBuilder(): this(20)
        {
        }

        public IEnumerable<Page> BuildPages(IEnumerable<Record> records)
        {

            List<Record> recordListe = new List<Record>();
            int i = 0;
            foreach (Record record in records)
            {

                recordListe.Add(record);
                if(i++ == pageSize-1)
                {
                    i = 0;
                    yield return new Page(recordListe);
                    recordListe.Clear();
                    
                }
            }

            yield return new Page(recordListe);
        }
    }
}
